package com.newdjk.newdoctor.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;


/**
 * Initial the camera
 * <p>
 * 默认的二维码扫描Activity
 */
public class CaptureActivity extends AppCompatActivity implements QRCodeView.Delegate {


    private static final String TAG = "CaptureActivity";
    @BindView(R.id.zbarview)
    ZXingView zbarview;

    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        MyApplication.mActivities.add(this);
        setContentView(R.layout.camera);
        ButterKnife.bind(this);
        setDarkStatusWhite(false);
        type = getIntent().getIntExtra("type", 0);
        zbarview.setDelegate(this);
        zbarview.startCamera();//打开相机
        zbarview.showScanRect();//显示扫描框
        zbarview.startSpot();//开始识别二维码

    }

    /**
     * 状态栏白底黑字
     */
    protected void setDarkStatusWhite(boolean bDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            getWindow().setStatusBarColor(getResources().getColor(android.R.color.black));//这里对应的是状态栏的颜色，就是style中colorPrimaryDark的颜色
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (bDark) {
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

    @Override
    protected void onStop() {
        zbarview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zbarview.onDestroy();
        MyApplication.remove(this);
        super.onDestroy();
    }


    @Override
    protected void onPause() {
        super.onPause();
        zbarview.stopCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        zbarview.startCamera();//打开相机
        zbarview.showScanRect();//显示扫描框
        zbarview.startSpot();//开始识别二维码
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.d(TAG, "扫描结果" + result);
        Intent intent = new Intent();
        intent.putExtra("code", result);
        this.setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        MyApplication.remove(this);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (type == 12) {
                MyApplication.exit();
            } else {
                finish();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}